package cn.jbolt.wechat.common;

import me.chanjar.weixin.common.error.WxErrorException;

public class WechatApiResult<T> {

    /**
     * 信息
     */
    private String msg;
    /**
     * 操作是否成功
     */
    private boolean success = true;
    /**
     * 异常
     */
    private Throwable error;
    /**
     * 绑定的数据
     */
    private T data;

    public WechatApiResult() {}

    public WechatApiResult(String msg) {
        this.msg = msg;
    }

    public WechatApiResult(String msg, Throwable error) {
        this.msg = msg;
        if (error instanceof WxErrorException) {
            this.msg += ((WxErrorException)error).getError().toString();
        }
        this.success = false;
        this.error = error;
    }

    public T getData() {
        return data;
    }

    public WechatApiResult setData(T data) {
        this.data = data;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Throwable getError() {
        return error;
    }

    public void setError(Throwable error) {
        this.error = error;
    }

    /**
     * 获取完整的信息
     * @return
     */
    public String getFullMsg() {
        String full = "";
        if (this.msg != null) {
            full += this.msg + "\n";
        }
        if (this.error != null) {
            full += error.toString() + "\n";
        }
        return full;
    }
}
